package com.woniuxy.exam.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.dto.QuestionDto;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.SingleChoice;
import com.woniuxy.commons.utils.TimeUtils;
import com.woniuxy.exam.service.SingleChoiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zouyang
 * @date 2020/7/17 - 23:30
 */
@RestController
public class SingleChoiceController {

    @Autowired
    private SingleChoiceService singleChoiceService;

    @GetMapping("/sc/all")
    public List<SingleChoice> getAll(){
        return singleChoiceService.selectAll();
    }

    @GetMapping("/sc/allByPage")
    public ResponseEntity getAllByPage(){
        PageHelper.startPage(1,10);
        List<SingleChoice> list = singleChoiceService.selectAll();
        PageInfo<SingleChoice> pageInfo = new PageInfo<>(list,5);
        return new ResponseEntity(200,"success",pageInfo);
    }

    @PostMapping("/sc/add")
    public ResponseEntity add(@RequestBody SingleChoice singleChoice){
        singleChoice.setCreateDate(TimeUtils.getNowTime());
        int res = singleChoiceService.insert(singleChoice);
        if(res!=1){
            return new ResponseEntity(500,"系统繁忙");
        }
        return ResponseEntity.SUCCESS_RESPONSE;
    }

    @RequestMapping("/sc/search")
    public List<SingleChoice> search(QuestionDto questionDto){
        SingleChoice singleChoice = new SingleChoice();
        BeanUtils.copyProperties(questionDto,singleChoice);
        return singleChoiceService.selectByAll(singleChoice);
    }

    @GetMapping("/sc/find/{id}")
    public SingleChoice find(@PathVariable("id") int id){
        return singleChoiceService.selectByPrimaryKey(id);
    }

    @PostMapping("/sc/caps")
    public ResponseEntity conditionAndPageSearch(QuestionDto questionDto){
        SingleChoice singleChoice = new SingleChoice();
        BeanUtils.copyProperties(questionDto,singleChoice);
        PageHelper.startPage(questionDto.getPageNum(),questionDto.getPageSize());
        List<SingleChoice> list =  singleChoiceService.selectByAll(singleChoice);
        PageInfo pageInfo = new PageInfo(list,10);
        return new ResponseEntity(200,"success",pageInfo);
    }

    @RequestMapping("/sc/update")
    public ResponseEntity update(@RequestBody SingleChoice singleChoice){
        int res = singleChoiceService.updateByPrimaryKeySelective(singleChoice);
        if(res!=1){
            return new ResponseEntity(500,"系统繁忙");
        }
        return ResponseEntity.SUCCESS_RESPONSE;
    }

    @RequestMapping("/sc/delete")
    public ResponseEntity delete(int id){
        int res = singleChoiceService.deleteByPrimaryKey(id);
        if(res!=1){
            return new ResponseEntity(500,"出现异常");
        }
        return new ResponseEntity(200,"success");
    }
}
